{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Image Functions 2\n", "\n", "To work with images in Jupyter Processing, you need to do three things:\n", "\n", "1. Upload your images to the same folder where your notebook is\n", "1. List the images in a @pjs comment (see below)\n", "3. use loadImage() to load the image into Processing" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_1\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_1\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #1:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #1 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "/* @pjs preload=\"pic3.jpg,tornadovolcano.jpg\"; */\n", "\n", "PImage img;\n", "PImage bimg;\n", "\n", "void setup() {\n", " int guess = 13;\n", " img = loadImage(\"pic3.jpg\");\n", " bimg = loadImage(\"tornadovolcano.jpg\");\n", " img.resize(600, 400);\n", " bimg.resize(600, 400);\n", " size(img.width, img.height);\n", " img.loadPixels();\n", " bimg.loadPixels();\n", " for (int y=0; y < img.height; y++) {\n", " for (int x=0; x < img.width; x++) {\n", " color c = img.pixels[x + y * img.width];\n", " float r = red(c);\n", " float g = green(c);\n", " float b = blue(c);\n", " if (\n", " (((201 - guess) < r && r < (201 + guess)) && \n", " ((59 - guess) < g && g < (59 + guess)) && \n", " ((98 - guess) < b && b < (98 + guess)))\n", " ||\n", " (((180 - guess) < r && r < (211 + guess)) && \n", " ((141 - guess) < g && g < (141 + guess)) && \n", " ((151 - guess) < b && b < (151 + guess)))\n", " || // 162, 113, 116\n", " (((162 - guess) < r && r < (162 + guess)) && \n", " ((113 - guess) < g && g < (113 + guess)) && \n", " ((120 - guess) < b && b < (116 + guess)))\n", " ||\n", " (((123 - guess) < r && r < (123 + guess)) && \n", " ((77 - guess) < g && g < (77 + guess)) && \n", " ((80 - guess) < b && b < (80 + guess)))) {\n", " img.pixels[x + (y * img.width)] = bimg.pixels[x + (y * bimg.width)];\n", " }\n", " }\n", " } \n", " img.updatePixels(); \n", "}\n", "\n", "void mousePressed() {\n", " img.loadPixels();\n", " color c = img.pixels[mouseX + mouseY * img.width]; // img.get(mouseX, mouseY);\n", " float r = red(c);\n", " float g = green(c);\n", " float b = blue(c);\n", " println(\"RGB: (\" + mouseX + \", \" + mouseY + \") \" + r + \", \" + g + \", \" + b);\n", "}\n", "\n", "void draw() {\n", " image(img, 0, 0);\n", " noLoop();\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_2\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_2\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_2\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_2\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #2:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #2 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "/* @pjs preload=\"pic3.jpg\"; */\n", "// (34, 89) \n", "// (135, 186) \n", "\n", "PImage img;\n", "PImage head;\n", "\n", "Head[] heads;\n", "\n", "void setup() {\n", " img = loadImage(\"pic3.jpg\");\n", " head = createImage(135 - 34, 186 - 89, RGB);\n", " img.resize(600, 400);\n", " size(img.width, img.height);\n", " img.loadPixels();\n", " head.loadPixels();\n", " for (int x=34; x < 135; x++) {\n", " for (int y=89; y < 186; y++) {\n", " head.pixels[x - 34 + (y - 89) * head.width] = img.pixels[x + y * img.width];\n", " }\n", " } \n", " head.updatePixels(); \n", " head.resize(30, 30);\n", " heads = new Head[100];\n", " for (int i =0; i < 100; i++) {\n", " heads[i] = new Head(random(width), random(height));\n", " }\n", " \n", "}\n", "\n", "class Head {\n", " float hx, hy, vx, vy;\n", " \n", " Head(float hx, float hy) {\n", " this.hx = hx;\n", " this.hy = hy;\n", " this.vx = random(5) - 2.5;\n", " this.vy = random(5) - 2.5;\n", " }\n", " \n", " void move() {\n", " hx += vx;\n", " hy += vy;\n", " if (hx > width)\n", " vx = -vx;\n", " else if (hx < 0)\n", " vx = -vx;\n", " if (hy > height)\n", " vy = -vy;\n", " else if (hy < 0)\n", " vy = -vy;\n", " }\n", " \n", " void draw() {\n", " image(head, hx, hy);\n", " }\n", "}\n", "\n", "void draw() {\n", " background(255);\n", " for (int i =0; i < 100; i++) {\n", " heads[i].move();\n", " heads[i].draw();\n", " }\n", "}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "java", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 1 }